package roborally.util;

import be.kuleuven.cs.som.annotate.Basic;
import be.kuleuven.cs.som.annotate.Raw;
/**
 * This is an enum representing boolean operators.

 * @version 3.0
 * @author Bart Opsomer 2de Bachelor Ingenieurswetenschappen Electrotechniek-Computerwetenschappen 
 * 		   Peter Opsomer 1ste Master Wiskundige IngenieursTechnieken
 */
public enum CommandWordBooleanOperator{
	// all the possible enums of this class, with their implemented methods
	 AND("and", 0) {
		@Override
		public boolean evaluate(Boolean[] args) {
			for( boolean b : args) {
				if(!b)
					return false;
			}
			return true;
		}
	}
	, OR("or", 0) {
		@Override
		public boolean evaluate(Boolean[] args) {
			for( boolean b : args) {
				if(b)
				return true;
			}
			return false;
		}
	}
	, NOT("not",1) {
		@Override
		public boolean evaluate(Boolean[] args) {
			if(args.length == 1) {
				return !args[0];
			}
			return false;
		}
	};
	// the textual representation of the String an its number of arguments
	private String text;
	private int nbArgs;
	/**
	 * the constructor of the enum
	 * @param word the textual representation of the enum
	 * @param nbArgs the number of arguments of its evaluate method
	 */
	@Raw
	CommandWordBooleanOperator(String word,int nbArgs){
		this.text = word;
		this.nbArgs = nbArgs;
	}
	/**
	 * @return the text of this CommandWordBooleanOperator
	 */
	@Basic
	public String getText() {
		return this.text;
	}
	/**
	 * @return the number of arguments of this CommandWordBooleanOperator, 0 meaning number not known
	 */
	@Basic
	public int getNumberOfArguments() {
		return this.nbArgs;
	}
	/**
	 * Get the CommandWordBooleanOperator that the given String represents.
	 * 
	 * @param condition the String representing an CommandWordsCondition
	 * @return | result == null || result.getText().equals(condition)
	 */
	public static CommandWordsCondition fromString(String condition) {
		if (condition != null) {
			for (CommandWordsCondition c : CommandWordsCondition.values()) {
				if (condition.equals(c.getText())) {
					return c;
				}
			}
		}
		return null;
	}
	/**
	 * The evaluate method of this class, implemented by all members
	 * @param args the array of boolean arguments
	 * @return the boolean result of the members boolean function
	 */
	public abstract boolean evaluate(Boolean[] args);
}
